package com.wb.utils

import com.wb.component.Slf4j
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import org.jsoup.Jsoup
import kotlin.jvm.Throws

/**
 * A站 工具
 *
 * @author 祁雪
 */
@Slf4j
class AcFunUtil {

    companion object {

        fun search(key: String): ArrayList<SearchData> {

            val url = "https://www.acfun.cn/search?sortType=4&channelId=0&type=video&keyword=${key}"

//            val doc = RestUtil.getPage(
//                url, hashMapOf(
//                    "type" to "video",
//                    "keyword" to "misa"
//                )
//            )

            val doc = Jsoup.connect(url).get()

            val elements = doc.select("script")

            var html = ""

            try {
                elements.forEach {
                    if (it.html().contains("pagelet_video")) {
                        html = it.html().substring(it.html().indexOf("{"), it.html().lastIndexOf("}") + 1)
                        throw RuntimeException("已找到内容")
                    }
                }
            } catch (e: Exception) {
                println(e.message)
            }

            if (html.isBlank()) {
                return arrayListOf()
            }
            println("开始解析")

            val gson = Gson()

            val map = gson.fromJson(
                html,
                object : TypeToken<HashMap<String, Any>>() {}.type
            ) as HashMap<String, Any>

            if (map.getOrDefault("html", "").toString().isBlank()) {
                return arrayListOf()
            }

            val htmlDoc = Jsoup.parse(map.get("html").toString())

            val cards = htmlDoc.select(".search-video-card")

            val list = arrayListOf<SearchData>()
            cards.forEach {

                val data = SearchData()

                val img = it.select(".video__cover").select("img")
                if (img != null && img.hasAttr("src")) {
                    data.img = img.attr("src")
                }

                val url = it.selectFirst("a").attr("href")
                data.goUrl = "https://www.acfun.cn${url}"

                data.title = it.select(".video__main__title").text()

                data.up = it.select(".video__main__user").select(".user-name").text()

                data.play = it.select(".info__view-count").text()
                data.date = it.select(".info__create-time").text()

                list.add(data)
            }

            return list
        }

    }

    class SearchData {

        /**
         * 图片地址
         */
        var img: String = ""

        var title: String = "";

        var up: String = "";

        var play: String = "";

        var date: String = "";

        var goUrl: String = "";

        override fun toString(): String {
            return "SearchData(img='$img', title='$title', up='$up', play='$play', date='$date', goUrl='$goUrl')"
        }

    }

}

fun main() {
    AcFunUtil.search("旗袍").forEach {
        println(it)
    }
}